package org.dfl.plugin.editor.commands;

import org.dfl.messages.Messages;
import org.dfl.plugin.editor.model.ConnectionModelElement;
import org.eclipse.gef.commands.Command;

/**
 * command that disconnects connections
 * @author Piotr Wlodarczyk
 *
 */
public class ConnectionDeleteCommand extends Command {

	/** Connection instance to disconnect. */
	private final ConnectionModelElement connection;

	/**
	 * Create a command that will disconnect a connection from its endpoints.
	 * 
	 * @param conn
	 *            the connection instance to disconnect (non-null)
	 * @throws IllegalArgumentException
	 *             if conn is null
	 */
	public ConnectionDeleteCommand(ConnectionModelElement conn) {
		if (conn == null) {
			throw new IllegalArgumentException();
		}
		setLabel(Messages.ConnectionDeleteCommand_label);
		this.connection = conn;
	}
	
	@Override
	public boolean canExecute() {
		if(connection.getSource().getDiagram() == null || !connection.getSource().getDiagram().isEditable()) 
			return false;
		return connection!=null;
	}
	
	@Override
	public boolean canUndo() {
		return (connection!=null && connection.getSource().getDiagram().isEditable());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute() {
		connection.disconnect();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	public void undo() {
		connection.reconnect();
	}
}
